import { createRouter, createWebHistory } from 'vue-router'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import systemRouter from './systemRouter'
import { hasRole } from '@/utils/hasRole.js'

import { message } from "ant-design-vue";

import tool from '@/utils/tool'
import { cloneDeep } from 'lodash-es'

/* import { menuStore } from '@/store/menu'
const useMenuStore = menuStore() */
// 进度条配置
NProgress.configure({ showSpinner: false, speed: 500 })


// 系统特殊路由
const routes_404 = [
	{
		path: '/:pathMatch(.*)*',
		hidden: true,
		component: () => import('@/views/NotFound/index.vue')
	},

]
// 系统路由
const routes = [...systemRouter, ...routes_404]

const router = createRouter({
	history: createWebHistory(),
	routes
})

router.beforeEach(async (to, from, next) => {
	if (to.fullPath.includes('/realTimeMonitoring')) {
		document.title = '安徽能通实时监控系统'
	} else {
		document.title = '安徽能通MES系统'
	}
	NProgress.start()
	/* if (to.fullPath === '/login') {
		if (localStorage.getItem('UUID')) {

			router.replace('/')
		}
	} */
	if (to.fullPath === '/noPermission') {
		return
	}

	if ((to.fullPath != '/login') && !(to.fullPath.includes('/realTimeMonitoring')) && !(to.fullPath.includes('/index')) && !(to.fullPath.includes('/noPermission '))) {
		if (!localStorage.getItem('UUID')) {
			router.replace('/login')
		}
	}
	if ((to.fullPath != '/login') && !(to.fullPath.includes('/realTimeMonitoring')) && !(to.fullPath.includes('/index')) && !(to.fullPath.includes('/noPermission '))) {
		let MgrModules = JSON.parse(localStorage.getItem('MgrModules'))
		let childModules
		MgrModules.forEach(item => {
			if (to.fullPath.includes(item.ModuleUrl)) {
				childModules = item.Childs
			}
		})
		if (childModules[0]) {
			childModules.forEach(item => {
				if (to.fullPath === item.ModuleUrl) {
					let bool = hasRole(item.RolePower)
					if (!bool) {
						message.error('暂无权限')
						router.push('/noPermission')
					}

				} else {

				}
			})
		}
	}
	next()



})
router.afterEach((to, from) => {
	NProgress.done()
})

router.onError((error) => {
	NProgress.done()
	console.error({
		message: '路由错误',
		description: error.message
	})
})

export default router
